/*
Copyright 2020 Mirantis, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package token

import (
	"context"
	"fmt"
	"time"

	"github.com/sirupsen/logrus"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"

	"github.com/k0sproject/k0s/internal/util"
	k8sutil "github.com/k0sproject/k0s/pkg/kubernetes"
)

// NewManager creates a new token manager using given kubeconfig
func NewManager(kubeconfig string) (*Manager, error) {
	logrus.Debugf("loading kubeconfig from: %s", kubeconfig)
	client, err := k8sutil.Client(kubeconfig)
	if err != nil {
		return nil, err
	}
	return &Manager{
		client: client,
	}, nil
}

// Manager is responsible to manage the join tokens in kube API as secrets in kube-system namespace
type Manager struct {
	client kubernetes.Interface
}

// Create creates a new bootstrap token
func (m *Manager) Create(valid time.Duration, role string) (string, error) {
	tokenID := util.RandomString(6)
	tokenSecret := util.RandomString(16)

	token := fmt.Sprintf("%s.%s", tokenID, tokenSecret)

	data := make(map[string]string)
	data["token-id"] = tokenID
	data["token-secret"] = tokenSecret
	if valid != 0 {
		data["expiration"] = time.Now().Add(valid).UTC().Format(time.RFC3339)
		logrus.Debugf("Set expiry to %s", data["expiration"])
	}

	if role == "worker" {
		data["description"] = "Worker bootstrap token generated by k0s"
		data["usage-bootstrap-authentication"] = "true"
		data["usage-bootstrap-signing"] = "true"
	} else {
		data["description"] = "Controller bootstrap token generated by k0s"
		data["usage-bootstrap-authentication"] = "false"
		data["usage-bootstrap-signing"] = "false"
		data["usage-controller-join"] = "true"
	}

	secret := &v1.Secret{
		ObjectMeta: metav1.ObjectMeta{
			Name:      fmt.Sprintf("bootstrap-token-%s", tokenID),
			Namespace: "kube-system",
		},
		Type:       v1.SecretTypeBootstrapToken,
		StringData: data,
	}

	_, err := m.client.CoreV1().Secrets("kube-system").Create(context.TODO(), secret, metav1.CreateOptions{})
	if err != nil {
		return "", err
	}

	return token, nil
}
